{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45d56c67-7439-4264-912a-c0b4895cac63",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f71e64d0-f967-4244-98ba-4e5bc4530883",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "489900d6-cc33-4ada-b2be-7e3a139cf6ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "! cd modelscope-agent && pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e9f3150",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a027a6e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/llms/multi_llm_with_alpha_umi')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f35e3f7",
   "metadata": {},
   "source": [
    "### 部署模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad038344",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    import vllm\n",
    "except ImportError:\n",
    "    raise ImportError(\"The vllm package is not installed. Please make sure GPU env is ready and refer to https://docs.vllm.ai/en/latest/getting_started/installation.html\")\n",
    "    \n",
    "os.system(\"export VLLM_USE_MODELSCOPE=True\")\n",
    "os.system(\"python -m vllm.entrypoints.openai.api_server \\\n",
    " --model=iic/alpha-umi-planner-7b \\\n",
    " --revision=v1.0.0 --trust-remote-code \\\n",
    " --port 8090 \\\n",
    " --dtype float16 \\\n",
    " --gpu-memory-utilization 0.3 > planner.log &\")\n",
    "\n",
    "os.system(\"python -m vllm.entrypoints.openai.api_server \\\n",
    " --model=iic/alpha-umi-caller-7b \\\n",
    " --revision=v1.0.0 --trust-remote-code \\\n",
    " --port 8091 \\\n",
    " --dtype float16 \\\n",
    " --gpu-memory-utilization 0.3 > caller.log &\")\n",
    "\n",
    "os.system(\"python -m vllm.entrypoints.openai.api_server \\\n",
    " --model=iic/alpha-umi-summarizer-7b \\\n",
    " --revision=v1.0.0 --trust-remote-code \\\n",
    " --port 8092 \\\n",
    " --dtype float16 \\\n",
    " --gpu-memory-utilization 0.3 > summarizer.log &\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3de23896",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65e5dcc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n",
    "print('请输入ModelScope Token')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()\n",
    "print('请输入RapidAPI Token')\n",
    "os.environ['RAPID_API_TOKEN'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c8defa3",
   "metadata": {},
   "source": [
    "### 构建agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01e90564",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.agents.alpha_umi import AlphaUmi\n",
    "\n",
    "\n",
    "llm_configs = {\n",
    "    'planner_llm_config': {\n",
    "        'model': 'iic/alpha-umi-planner-7b',\n",
    "        'model_server': 'openai',\n",
    "        'api_base': 'http://localhost:8090/v1',\n",
    "        'is_chat': False\n",
    "    },\n",
    "    'caller_llm_config': {\n",
    "        'model': 'iic/alpha-umi-caller-7b',\n",
    "        'model_server': 'openai',\n",
    "        'api_base': 'http://localhost:8091/v1',\n",
    "        'is_chat': False\n",
    "    },\n",
    "    'summarizer_llm_config': {\n",
    "        'model': 'iic/alpha-umi-summarizer-7b',\n",
    "        'model_server': 'openai',\n",
    "        'api_base': 'http://localhost:8092/v1',\n",
    "        'is_chat': False\n",
    "    },\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "function_list = [\"get_data_fact_for_numbers\", \"get_math_fact_for_numbers\", \"get_year_fact_for_numbers\",\n",
    "                    \"listquotes_for_current_exchange\",\n",
    "                    \"exchange_for_current_exchange\"]\n",
    "\n",
    "\n",
    "bot = AlphaUmi(\n",
    "        function_list=function_list,\n",
    "        llm_planner=llm_configs['planner_llm_config'],\n",
    "        llm_caller=llm_configs['caller_llm_config'],\n",
    "        llm_summarizer=llm_configs['summarizer_llm_config'],\n",
    "    )\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "064ad74e",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdd379ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run('how many CNY can I exchange for 3.5 US dollar? \\\n",
    "                   also, give me a special property about the number of CNY after exchange')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85434381",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
